<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Redis 常用命令 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Redis 常用命令</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<h2 id="1-string-常用操作"><a href="#1-string-常用操作" class="headerlink" title="1.string 常用操作"></a>1.string 常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span> key1 yanyi</div><div class="line">get key1  </div><div class="line"><span class="built_in">set</span> key1 yanyi1           <span class="comment"># 一个key对应一个value，多次赋值，会覆盖前面的value</span></div><div class="line">setnx key2 aaa            <span class="comment"># 返回1</span></div><div class="line">get key2</div><div class="line">setnx key2 bbb            <span class="comment"># 返回0;说明：setnx 如果key存在，则返回0，不存在会直接创建这个key</span></div><div class="line">setex key3 10 1           <span class="comment"># 这个是用来给key设定过期时间的</span></div><div class="line">mset key1 1 key2 2 key3 3 <span class="comment"># 同时设置多个key</span></div><div class="line">mget key1 key2 key3</div></pre></td></tr></table></figure>
<h2 id="2-hash-数据常用操作"><a href="#2-hash-数据常用操作" class="headerlink" title="2.hash 数据常用操作"></a>2.hash 数据常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">hset user1  name aming               <span class="comment"># 建立hash</span></div><div class="line">hset user1 age 30</div><div class="line">hset user1 job  it</div><div class="line">hgetall user1</div><div class="line">hmset user2 name yanyi age 30 job it <span class="comment"># 批量建立键值对</span></div><div class="line">hmget user2</div><div class="line">hmget user2 name age  job</div><div class="line">hdel user2 job                       <span class="comment"># 删除指定filed</span></div><div class="line">hkeys user2                          <span class="comment"># 打印所有的key</span></div><div class="line">hvals user2                          <span class="comment"># 打印所有的values</span></div><div class="line">hlen user2                           <span class="comment"># 查看hash有几个filed</span></div></pre></td></tr></table></figure>
<h2 id="3-list-数据常用操作"><a href="#3-list-数据常用操作" class="headerlink" title="3.list 数据常用操作"></a>3.list 数据常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">lpush lista  a                 <span class="comment"># 从左侧压入一个元素</span></div><div class="line">lpush lista b</div><div class="line">lrange lista  0 -1             <span class="comment"># 0 为头，-1为尾</span></div><div class="line">lpop lista                     <span class="comment"># 从左侧取出</span></div><div class="line">rpush lista  1                 <span class="comment"># 从右侧压入一个元素</span></div><div class="line">rpush lista  2</div><div class="line">lrange lista  0 -1</div><div class="line">rpop lista                     <span class="comment"># 从右侧取第一个元素</span></div><div class="line">linsert  lista  before  2 3    <span class="comment"># 在2的前面插入一个元素为3</span></div><div class="line">lset lista 4 bbb               <span class="comment"># 把第5个元素修改为bbb</span></div><div class="line">lindex lista 0                 <span class="comment"># 查看第1个元素</span></div><div class="line">lindex lista 3                 <span class="comment"># 查看第4个元素</span></div><div class="line">llen lista                     <span class="comment"># 查看链表中有几个元素</span></div></pre></td></tr></table></figure>
<h2 id="4-set-数据常用操作"><a href="#4-set-数据常用操作" class="headerlink" title="4.set 数据常用操作"></a>4.set 数据常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">sadd seta aaa               <span class="comment">#向集合seta中放入元素</span></div><div class="line">smembers seta               <span class="comment"># 查看集合中的所有元素</span></div><div class="line">srem  seta    aaa           <span class="comment"># 删除元素</span></div><div class="line">spop  seta                  <span class="comment"># 随机取出一个元素，删除</span></div><div class="line">sdiff  seta  setb           <span class="comment"># 求差集，以seta为标准</span></div><div class="line">sdiffstore setc seta setb   <span class="comment"># 求差集并且存储，存储到了setc里</span></div><div class="line">sinter seta setb            <span class="comment"># 求交集</span></div><div class="line">sinterstore  setd seta setb <span class="comment"># 将交集存储setd</span></div><div class="line">sunion seta setb            <span class="comment"># 求并集</span></div><div class="line">sunionstore sete seta setb  <span class="comment"># 求并集并存储到sete</span></div><div class="line">sismember seta aaa          <span class="comment"># 判断一个元素是否属于一个集合</span></div><div class="line">srandmember  seta           <span class="comment"># 随机取出一个元素，但不删除</span></div></pre></td></tr></table></figure>
<h2 id="5-zset-数据常用操作"><a href="#5-zset-数据常用操作" class="headerlink" title="5.zset 数据常用操作"></a>5.zset 数据常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">zadd zseta 11 123             <span class="comment"># 创建有序集合</span></div><div class="line">zrange zseta 0 -1             <span class="comment"># 显示所有元素，按顺序显示</span></div><div class="line">zrange zseta 0 -1 withscores  <span class="comment"># 可以带上分值</span></div><div class="line">zrem zseta 222                <span class="comment"># 删除指定元素</span></div><div class="line">zrank zseta  222              <span class="comment"># 返回元素的索引值，索引值从0开始，按score正向排序</span></div><div class="line">zrevrank zseta 222            <span class="comment"># 同上，不同的是，按score反序排序</span></div><div class="line">zrevrange  zseta  0 -1        <span class="comment"># 反序显示所有元素，并带分值</span></div><div class="line">zcard zseta                   <span class="comment"># 返回集合中所有元素的个数</span></div><div class="line">zcount  zseta 1 10            <span class="comment"># 返回分值范围1-10的元素个数</span></div><div class="line">zrangebyscore  zseta 1 10     <span class="comment"># 返回分值范围1-10的元素</span></div><div class="line">zremrangebyrank zseta  0 2    <span class="comment"># 删除索引范围0-2的元素，按score正向排序</span></div><div class="line">zremrangebyscore zseta  1 10  <span class="comment"># 删除分值范围1-10的元素</span></div></pre></td></tr></table></figure>
<h2 id="6-键值相关操作"><a href="#6-键值相关操作" class="headerlink" title="6.键值相关操作"></a>6.键值相关操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">keys *                 <span class="comment"># 取出所有key</span></div><div class="line">keys my*               <span class="comment"># 模糊匹配</span></div><div class="line">exists name            <span class="comment"># 有name键 返回1 ，否则返回0；</span></div><div class="line">del  key1              <span class="comment"># 删除一个key，成功返回1 ，否则返回0；</span></div><div class="line">EXPIRE key1 100        <span class="comment"># 设置key1 100s后过期</span></div><div class="line">ttl key                <span class="comment"># 查看键 还有多长时间过期，单位是s,当 key 不存在时，返回 -2 。 当 key 存在但没有设置剩余生存时间时，返回 -1 。 否则，返回 key 的剩余生存时间。</span></div><div class="line">select  0              <span class="comment"># 代表选择当前数据库，默认进入0 数据库</span></div><div class="line">move age 1             <span class="comment"># 把age 移动到1 数据库</span></div><div class="line">persist key1           <span class="comment"># 取消key1的过期时间</span></div><div class="line">randomkey              <span class="comment"># 随机返回一个key</span></div><div class="line">rename oldname newname <span class="comment"># 重命名key</span></div><div class="line"><span class="built_in">type</span> key1              <span class="comment"># 返回键的类型</span></div></pre></td></tr></table></figure>
<h2 id="7-服务相关操作"><a href="#7-服务相关操作" class="headerlink" title="7.服务相关操作"></a>7.服务相关操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">dbsize   <span class="comment"># 返回当前数据库中key的数目</span></div><div class="line">info     <span class="comment"># 返回redis数据库状态信息</span></div><div class="line">flushdb  <span class="comment"># 清空当前数据库中所有的键</span></div><div class="line">flushall <span class="comment"># 清空所有数据库中的所有的key</span></div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Redis/4. Redis 常用命令/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
